//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace OpenLS.Chart.UI
{
    class NumericValueToArcDataConverter: IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            NumericValue v = (NumericValue)values[0];
            double width = (double)values[1];
            double height = (double)values[2];
            ChartBase i = (ChartBase)values[3];
            if (i == null)
                return null;
            double startAngle = Math.PI / 2;
            if (values[3] is PieChart)
            {
                startAngle -= ((PieChart)values[3]).FirstSliceAngle * Math.PI / 180d;
            }
            PieSeries series = (PieSeries)values[4];
            double explosion = series.Explosion;
            int valueCount = ((new List<NumericValue>(series.Values.NumericValues))).Count;
            ChartLayout info = ChartToChartInfoConverter.GetInfo(i);
            PathGeometry result = new PathGeometry();
            PathFigure figure = new PathFigure();
            Bounds bounds = info.GetBounds(v.Index, 0);
            double dim = Math.Min(width, height) / 2;

            double len = dim / (1 + explosion / 100);
            double angle1 = startAngle - 2 * Math.PI * bounds.Low;
            double angle2 = startAngle - 2 * Math.PI * bounds.High;
            double midAngle = ((angle1 + angle2)) / 2;
            Point center = new Point(dim, dim);
            double mult = explosion / 100; //\\ *(valueCount - v.Index) / valueCount;
            center.Offset(len * mult * Math.Cos(midAngle), -len * mult * Math.Sin(midAngle));
            figure.StartPoint = center;
            Point pt1 = new Point(center.X + len * Math.Cos(angle1), center.Y - len * Math.Sin(angle1));
            Point pt2 = new Point(center.X + len * Math.Cos(angle2), center.Y - len * Math.Sin(angle2));

            figure.Segments.Add(new LineSegment(pt1,true));
            ArcSegment a = new ArcSegment();
            a.Point = pt2;
            a.Size = new Size(len, len);
            a.IsLargeArc = bounds.Delta > 0.5;
            a.SweepDirection = SweepDirection.Clockwise;
            figure.Segments.Add(a);
            figure.Segments.Add(new LineSegment(center, true));
            result.Figures.Add(figure);
            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}
